var Dragable = {
	movingLeft: 0,
	movingTop: 0,
	setMovingLeft: function(l) {
		this.movingLeft = l;
		return this;
	},
	setMovingTop: function(t) {
		this.movingTop = t;
		return this;
	},
	getMovingLeft: function() {
		return this.movingLeft;
	},
	getMovingTop: function() {
		return this.movingTop;
	},
	init: function(d) {
		var _this = this,
			moveBar = d.moveBar,
			moveBox = d.moveBox || moveBar,
			onMoveStart = d.onMoveStart,
			onMoving = d.onMoving,
			onMoved = d.onMoved;

		function handleDragable(){
			onMoveStart&&onMoveStart(moveBox);
			var e = e || window.event,
				x0 = e.clientX - moveBox.offsetLeft,
				y0 = e.clientY - moveBox.offsetTop;

			// if (moveBox.setCapture) {
			// 	moveBox.setCapture();
			// }
			document.onmousemove = function(e) {
				var e = e || window.event,
					x1 = e.clientX - x0,
					y1 = e.clientY - y0;
					if (x1==0) {

					}
				_this.movingLeft = x1;
				_this.movingTop = y1;
				if (onMoving) {
					onMoving(moveBox,{
						left:_this.movingLeft,
						top:_this.movingTop
					});
				}
				moveBox.style.left = _this.movingLeft + 'px';
				moveBox.style.top = _this.movingTop + 'px';
				return false;
			}
			document.onmouseup = function() {
				if (onMoved) {
					onMoved(moveBox,{
						left:_this.movingLeft,
						top:_this.movingTop
					});
				}
				// if (moveBar.releaseCapture) {
				// 	moveBar.releaseCapture();
				// }
				document.onmousemove = null;
				document.onmouseup = null;
			}
			return false;
		}	
		moveBar.addEventListener?
		moveBar.addEventListener('mousedown',handleDragable,false):
		moveBar.attachEvent('onmousedown',handleDragable);
	}
}

module.exports = Dragable;